package com.google
{
	/**
	 * ...
	 * @author db0@qq.com
	 */
	import com.google.zxing.BarcodeFormat;
	import com.google.zxing.BinaryBitmap;
	import com.google.zxing.BufferedImageLuminanceSource;
	import com.google.zxing.client.result.ResultParser;
	import com.google.zxing.common.BitMatrix;
	import com.google.zxing.common.flexdatatypes.HashTable;
	import com.google.zxing.common.GlobalHistogramBinarizer;
	import com.google.zxing.DecodeHintType;
	import com.google.zxing.MultiFormatWriter;
	import com.google.zxing.qrcode.QRCodeReader;
	import com.google.zxing.Result;
	import com.logs;
	import flash.system.Security;
	//import com.logs;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	
	public class qrcode extends Sprite
	{
		public function qrcode()
		{
			try {
				Security.allowDomain("*");
			}catch (e:Error) {
				logs.adds(e);
			}
			
			
			//refreshCode();
		}
		
		public function str2bmp(str:String, _width:int=100, _height:int=100,color:uint=0xff000000,bgColor=0xffffffff):Bitmap
		{
			return refreshCode(str, _width, _height,color,bgColor);
		}
		
		public function bmp2str(bmp:Bitmap,color:uint=0xff000000):String
		{
			return decodeBitmapData(bmp.bitmapData,color);
		}
		
		
		private static  function getAllHints():HashTable
		{
			var ht:HashTable=new HashTable;
			ht.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
			return ht;
		}
		
		public static  function decodeBitmapData(bmpd:BitmapData,color:uint=0xff000000):String
		{
			var lsource:BufferedImageLuminanceSource=new BufferedImageLuminanceSource(bmpd);
			var bitmap:BinaryBitmap=new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(bmpd)));
			var res:Result=null;
			try {
				var qrReader:QRCodeReader = new QRCodeReader();
				res=qrReader.decode(bitmap, getAllHints());
			}catch (e:Error) {
				return null;
			}
			if (res == null) {
				return null;
			}else {
				var str:String = (ResultParser.parseResult(res).getDisplayResult());
			}
			var bytes:ByteArray = new ByteArray();
			bytes.writeMultiByte(str, "ISO-8859-1");
			trace(bytes, bytes.length);
			bytes.position = 0;
			trace( bytes.bytesAvailable);
			str = bytes.readMultiByte(bytes.bytesAvailable, "utf-8");
			//logs.adds(str);
			return str;
		}

		public static  function refreshCode(str:String,_width:int,_height:int,color:uint=0xff000000,bgColor:uint=0x00000000):Bitmap
		{
			var matrix:BitMatrix;
			var qrEncoder:MultiFormatWriter = new MultiFormatWriter();
			try {
				//str = "http://192.168.1.118/baby.doc在";
				matrix = BitMatrix(qrEncoder.encode(str, BarcodeFormat.QR_CODE, _width, _height));
				//matrix = BitMatrix(qrEncoder.encode("b x", BarcodeFormat.PDF417, 250, 250));
				//matrix = BitMatrix(qrEncoder.encode("b x", BarcodeFormat.CODE_39, 250, 250));
				//matrix = BitMatrix(qrEncoder.encode("b x", BarcodeFormat.CODE_128, 250, 250));
				//trace(str,str.length );
			}catch (e:Error){
				trace('err',e);
				return null;
			}
			//trace(matrix);
			var bmd:BitmapData = new BitmapData(_width, _height);
			for (var h:int = 0; h < _height; h++)
			{
				for (var w:int = 0; w < _width; w++)
				{
					if (matrix._get(w,h) == 0){
						bmd.setPixel32(w, h, bgColor);
					}else{
						bmd.setPixel32(w, h,color);
					}
				}
			}
			
			//[Embed(source = "../../../bin/QQ20130305145105.jpg")]
			//var Bitmaps:Class
			var qrImg:Bitmap = new Bitmap(bmd);
			//qrImg = new Bitmaps;
			//addChild(qrImg);
			//trace("------------");
			//decodeBitmapData(qrImg.bitmapData, qrImg.width, qrImg.height);
			return qrImg;
		}
		
	}
}
